package com.minimalist.common.feign.fallback.lindi;

import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.vo.enterpriseaccount.LindiEnterpriseAccountQueryVO;
import com.minimalist.common.entity.lindi.vo.enterpriseaccount.LindiEnterpriseAccountVO;
import com.minimalist.common.feign.client.lindi.LindiEnterpriseAccountFeignClient;
import com.minimalist.common.feign.config.BaseFallbackFactory;
import com.minimalist.common.response.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 林地企业账户Feign客户端降级工厂
 * 使用FallbackFactory可以获取到异常信息，便于问题排查
 *
 * @author minimalist
 */
@Component
public class LindiEnterpriseAccountFeignClientFallbackFactory extends BaseFallbackFactory<LindiEnterpriseAccountFeignClient> {
    private static final Logger log = LoggerFactory.getLogger(LindiEnterpriseAccountFeignClientFallbackFactory.class);


    @Override
    public LindiEnterpriseAccountFeignClient create(Throwable cause) {
        return new LindiEnterpriseAccountFeignClient() {
            @Override
            public R<?> addEnterpriseAccount(LindiEnterpriseAccountVO enterpriseAccountVO) {
                logServiceUnavailable("添加企业账户", enterpriseAccountVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "添加企业账户失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> updateEnterpriseAccountByEnterpriseAccountId(LindiEnterpriseAccountVO enterpriseAccountVO) {
                logServiceUnavailable("修改企业账户", enterpriseAccountVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "修改企业账户失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> deleteEnterpriseAccountByEnterpriseAccountId(Long enterpriseAccountId) {
                logServiceUnavailable("删除企业账户", enterpriseAccountId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "删除企业账户失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<LindiEnterpriseAccountVO> getEnterpriseAccountByEnterpriseAccountId(Long enterpriseAccountId) {
                logServiceUnavailable("获取企业账户详情", enterpriseAccountId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "获取企业账户详情失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<PageResp<LindiEnterpriseAccountVO>> getPageEnterpriseAccountList(LindiEnterpriseAccountQueryVO queryVO) {
                logServiceUnavailable("分页查询企业账户列表", queryVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "分页查询企业账户列表失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<String> getPasswordByEnterpriseMobile(String enterpriseMobile) {
                logServiceUnavailable("根据企业手机号查询密码", enterpriseMobile, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "根据企业手机号查询密码失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<List<LindiEnterpriseAccountVO>> getEnterpriseAccountList() {
                logServiceUnavailable("获取企业账号列表", null, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), 
                        "获取企业账号列表失败，服务不可用: " + cause.getMessage());
            }
        };
    }
}

